/* 
 计算 
从 2010-06-21 余额为 8181.88 起,
 在年利率为 0.36%的情况下,
到 2010-09-21 时的利息,其中之间的存取款情况如下,负数表示取款:
20100709 450
20100709 450
20100709 3997.57
20100714 450
20100730 450
20100801 -10
20100810 1703.02
20100815 -5200
20100908 2699.88
END.
*/
package org.demo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 工具类：货币
 * @author  
 * @date    2010-9-24
 * @file    org.demo.util.CurrencyTools.java
 */
public class CurrencyTools {
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args)throws Exception{
		// 计算工商银行的活期利息
//		float rs = calculateInterest(8181.88f, 0.36f, "20100621", "20100921");
//		System.out.println("result=" + rs);
		// 人民币数字转大写形式
		// 伍角
		System.out.println(getChineseCurrencyString("00.50"));
		// 陆分
		System.out.println(getChineseCurrencyString("00.06"));
		// 壹亿元零陆分
		System.out.println(getChineseCurrencyString("100000000.06"));
		// 捌仟壹佰捌拾壹元捌角捌分
		System.out.println(getChineseCurrencyString("8181.88"));
	}
	/**
	 * 计算活期利息
	 * @param saveMoney 存款金额
	 * @param rate      利率
	 * @param startDate 起存日期
	 * @param endDate   结束日期
	 * @return
	 * @throws ParseException 
	 */
	public static float calculateInterest(
			float saveMoney,float rate,
			String startDate,String endDate) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// 先计算上期余额的利息
		Date start = format.parse(startDate);
		Date end = format.parse(endDate);
		int days = (int)DateTools.subtract(start, end);
		float result = saveMoney * rate * 0.01f * days / 360;
		// 处理在起存日期和结束日期之间的存取款情况
		Scanner in = new Scanner(System.in);
		String date = null;
		while((date = in.next()) != null){
			if (date.equals("END.")){
				break;
			}
			// 
			start = format.parse(date);
			days = (int)DateTools.subtract(start, end);
			float money = in.nextFloat();
			result += money * rate * 0.01f * days / 360;
		}
		return Float.valueOf(result);
	}
    /**
	 * 返回人民币大写数字
	 * @param money
	 * @return
	 */
	public static String getChineseCurrencyString(String money){
	    char[] digital = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
	    char[] unit = {'拾','佰','仟'};
	    char[] unit2 = {'万','亿','万'};
	    char cnZero = '零';
	    // 
	    StringBuilder result = new StringBuilder("元");
	    int idotIndex = money.indexOf('.');
	    String sbefore = money.substring(0,idotIndex);	    
	    // --------------- before dot --------------//
	    // j is use to unit, k is use to unit2    
	    int j=0,k=0; 
	    int len = sbefore.length();
	    char oldC = '3', newC = '0';
	    boolean hasN = false, allZero = true;
	    for (int i=0; i<len; i++){
	        if (i != 0 && j == 0){
	            // 添加 unit2
	            if (hasN){
	                if (oldC == '0'){
	                    result.insert(0,cnZero);
	                }
	            } else if ((k % 2) == 0){
	                // 此时result == "万元", 需要删掉 '万' 字
	                result.deleteCharAt(0);
	            }
	            result.insert(0,unit2[k-1]);
	            hasN = false;
	        }
	        newC = sbefore.charAt(len - 1 - i);
	        if (newC != '0'){
                if (oldC == '0' && hasN){
                    result.insert(0,cnZero);
                }
                if (j != 0){
                    result.insert(0,unit[j-1]);
                }
                int num = Integer.parseInt(Character.toString(newC));
                result.insert(0,digital[num]);
                hasN = true;
                allZero = false;
	        }
	        oldC = newC;
	        j++;
	        if (j == 4){
                k++;
                j = 0;
	        }
	    }
	    if (allZero){
            result.delete(0,result.length());
            result.append("零元");
	    } else {
            if (!hasN){
                result.deleteCharAt(0);
            }
            if (result.charAt(0) == '零'){
                result.deleteCharAt(0);
            }
	    }
	    // --------------- after dot -----------------//
	    String safter = money.substring(idotIndex + 1);
	    len = safter.length();
	    char[] cornunit = {'角','分'};
	    char[] data = {'0','0'};
	    data[0] = safter.charAt(0);
	    if (len > 1){
            data[1] = safter.charAt(1);
	    }
	    if (data[0] == '0' && data[1] == '0'){            
            return result.append('整').toString();
	    } else if (allZero){
            result.delete(0,result.length());
	    }
	    // first number
	    int num = Integer.parseInt(Character.toString(data[0]));
	    char ch = digital[num];
	    if (num != 0){
            result.append(ch).append(cornunit[0]);
	    } else if (!result.toString().equals("")){
            result.append(ch);
	    }
	    // second number
	    num = Integer.parseInt(Character.toString(data[1]));
	    ch = digital[num];
	    if (num != 0){
            result.append(ch).append(cornunit[1]);
	    }
	    // return value
	    return result.toString();
	}
}
